%% Main function to generate tests
function tests = segmentZeroAxisTest
tests = functiontests(localfunctions);
end

%% Test Functions

function testRegionPropsNotStruct(testCase)
% Tests whether function throws any error and whether the error is correct
% when presented with a value of rp that is not of type struct

rp = 'test';

noErr = false;

try
    segmentXAxis(rp);
    noErr = true;
catch ME   
    
    errorMsg = ['Incorrect error message thrown. Expected message about rp being of type struct', ... 
                'not being empty, and containing field "Eccentricity"'];
    
    verifyMatches(testCase, ME.identifier, ...
    'segmentXAxis:ImproperInput', ...
    errorMsg);

end

% Checks that an error is thrown at all
verifyFalse(testCase, noErr, ...,
    'The function did not throw any error when presented with an invalid rp input')
    

end

function testRegionPropsEmpty(testCase)
% Tests whether function throws any error and whether the error is correct
% when presented with a value of rp that contains an "Eccentricity" field
% but no values

rp = struct('Eccentricity', {});

noErr = false;

try
    segmentXAxis(rp);
    noErr = true;
catch ME   
    
    errorMsg = ['Incorrect error message thrown. Expected message about rp being of type struct', ... 
                'not being empty, and containing field "Eccentricity"'];
    
    verifyMatches(testCase, ME.identifier, ...
    'segmentXAxis:ImproperInput', ...
    errorMsg);

end

% Checks that an error is thrown at all
verifyFalse(testCase, noErr, ...,
    'The function did not throw any error when presented with an invalid rp input')
    

end



function testRegionPropsNoEccentricityField(testCase)
% Tests whether function throws any error and whether the error is correct
% when presented with a value of rp that does not contain the field
% 'Eccentricity'

rp = struct('test', {5});

noErr = false;

try
    segmentXAxis(rp);
    noErr = true;
catch ME   
    
    errorMsg = ['Incorrect error message thrown. Expected message about rp being of type struct', ... 
                'not being empty, and containing field "Eccentricity"'];
    
    verifyMatches(testCase, ME.identifier, ...
    'segmentXAxis:ImproperInput', ...
    errorMsg);

end

% Checks that an error is thrown at all
verifyFalse(testCase, noErr, ...,
    'The function did not throw any error when presented with an invalid rp input')
    

end

function testMultipleHighEccentricity(testCase)
% Tests whether function correctly segments out field containing highest
% eccentricity >= 0.9999

rp = struct('Eccentricity', {0.889, 0.999, 0.99991, 0.99995});

actSolution = segmentXAxis(rp);
expSolution = struct('Eccentricity', 0.99995);
verifyEqual(testCase, actSolution, expSolution);

end

function testNoHighEccentricity(testCase)
% Tests whether function returns nothing if there are no eccentricities
% above 0.9999 in rp

rp = struct('Eccentricity', {0.889, 0.999, 0.9999});

actSolution = segmentXAxis(rp);
verifyEmpty(testCase, actSolution);
end

function testFilerMultipleColumns(testCase)
% Tests whether function correctly filters on Eccentricity rather than
% returning max Eccentricity

rp = struct('Eccentricity', {0.889, 0.999, 0.99995}, 'Test', {0.1, 0.2, 0.3});

actSolution = segmentXAxis(rp);
expSolution = struct('Eccentricity', 0.99995, 'Test', 0.3);
verifyEqual(testCase, actSolution, expSolution);
end